package com.test.deencredit.fragment_deencredit.ln_state_deencredit;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.deencredit.R;
import com.test.deencredit.base_deencredit.BaseLnFragment_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.OthersInfo_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.RpInfo_deencredit;
import com.test.deencredit.config_deencredit.CountryConfig_deencredit;
import com.test.deencredit.dialog_deencredit.CustomerServiceDialog_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

public class LnRpyFragment_deencredit extends BaseLnFragment_deencredit {

    private TextView totalRpCurrencyTopTv_deencredit, totalRpTopTv_deencredit, dueDateTopTv_deencredit;
    private View detailView_deencredit;
    private TextView lnAmountTv_deencredit, interestTv_deencredit, serviceFeeTv_deencredit, ovduDaysTv_deencredit, ovduPenaltyTv_deencredit;
    private View vatView_deencredit;
    private TextView vatTv_deencredit;
    private View collapseBtn_deencredit;
    private TextView collapseTv_deencredit;
    private ImageView collapseIv_deencredit;
    private Button payBtn_deencredit;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page_deencredit = "REPAY";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ln_rpy_deencredit, container, false);

        view.findViewById(R.id.flr_ib_customerservice).setOnClickListener(v -> new CustomerServiceDialog_deencredit(requireContext()).show());

        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.flr_refreshlayout);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"REFRESH_CLICK","");
            HttpUtil_deencredit.getInstance_deencredit().requestRpDetail_deencredit(requireActivity(),this::refreshDetails_deencredit);
        });

        totalRpCurrencyTopTv_deencredit = view.findViewById(R.id.flr_tv_totalrp_currency_top);
        totalRpTopTv_deencredit = view.findViewById(R.id.flr_tv_totalrp_top);
        dueDateTopTv_deencredit = view.findViewById(R.id.flr_tv_duedate_top);
        detailView_deencredit = view.findViewById(R.id.flr_view_detail);
        lnAmountTv_deencredit = view.findViewById(R.id.flr_tv_lnamount);
        interestTv_deencredit = view.findViewById(R.id.flr_tv_interest);
        serviceFeeTv_deencredit = view.findViewById(R.id.flr_tv_servicefee);
        ovduDaysTv_deencredit = view.findViewById(R.id.flr_tv_ovdudays);
        ovduPenaltyTv_deencredit = view.findViewById(R.id.flr_tv_ovdupenalty);
        vatView_deencredit = view.findViewById(R.id.flr_view_vat);
        vatTv_deencredit = view.findViewById(R.id.flr_tv_vat);
        collapseBtn_deencredit = view.findViewById(R.id.flr_btn_collapse);
        collapseTv_deencredit = view.findViewById(R.id.flr_tv_collapse);
        collapseIv_deencredit = view.findViewById(R.id.flr_iv_copllapse);
        payBtn_deencredit = view.findViewById(R.id.flr_btn_pay);

        init_deencredit();

        return view;
    }

    private void init_deencredit(){
        //折叠/展开
        collapseBtn_deencredit.setOnClickListener(v -> collapse_deencredit());

        payBtn_deencredit.setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SUBMIT_CLICK","");
            if (CountryConfig_deencredit.getInstance_deencredit().isOrderRepayH5_deencredit()) HttpUtil_deencredit.getInstance_deencredit().orderRepayH5_deencredit(requireActivity(),"NO",this::toPay_deencredit);
            else if (CountryConfig_deencredit.getInstance_deencredit().isGetOrderRepayH5_deencredit()) HttpUtil_deencredit.getInstance_deencredit().getOrderRepayH5_deencredit(requireActivity(),"NO",this::toPay_deencredit);
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        HttpUtil_deencredit.getInstance_deencredit().requestRpDetail_deencredit(requireActivity(),this::refreshDetails_deencredit);
    }

    private void collapse_deencredit(){

        if (detailView_deencredit.getVisibility() == View.VISIBLE){
            detailView_deencredit.setVisibility(View.GONE);
            collapseTv_deencredit.setText(requireContext().getString(R.string.ln_rpy_expand_details_deencredit));
            collapseIv_deencredit.setBackgroundResource(R.drawable.icon_check_bottom_deencredit);
        }else {
            detailView_deencredit.setVisibility(View.VISIBLE);
            collapseTv_deencredit.setText(requireContext().getString(R.string.ln_rpy_collapse_details_deencredit));
            collapseIv_deencredit.setBackgroundResource(R.drawable.icon_check_top_deencredit);
        }

    }

    @SuppressLint("SetTextI18n")
    public void refreshDetails_deencredit(){

        String currency = RpInfo_deencredit.getInstance().getCurrency_deencredit();

        totalRpCurrencyTopTv_deencredit.setText(currency);
        totalRpTopTv_deencredit.setText(RpInfo_deencredit.getInstance().getRepayAmount_deencredit());
        dueDateTopTv_deencredit.setText(RpInfo_deencredit.getInstance().getRepayDate_deencredit());
        lnAmountTv_deencredit.setText(RpInfo_deencredit.getInstance().getAmount_deencredit()+" "+currency);
        interestTv_deencredit.setText(RpInfo_deencredit.getInstance().getInterest_deencredit()+" "+currency);
        ovduDaysTv_deencredit.setText(RpInfo_deencredit.getInstance().getOverdueDay_deencredit());
        ovduPenaltyTv_deencredit.setText(RpInfo_deencredit.getInstance().getOverdueAmount_deencredit()+" "+currency);

        if (CountryConfig_deencredit.getInstance_deencredit().isCote_deencredit()){
            vatView_deencredit.setVisibility(View.VISIBLE);
            vatTv_deencredit.setText(RpInfo_deencredit.getInstance().getServiceVatFee_deencredit()+" "+currency);
        }else vatView_deencredit.setVisibility(View.GONE);

        if (RpInfo_deencredit.getInstance().isOvdu_deencredit()) ovduDaysTv_deencredit.setTextColor(Color.RED);
        else ovduDaysTv_deencredit.setTextColor(Color.BLACK);

        if (CountryConfig_deencredit.getInstance_deencredit().isFrench_deencredit()) serviceFeeTv_deencredit.setText(RpInfo_deencredit.getInstance().getRepayProFee_deencredit()+" "+currency);
        else serviceFeeTv_deencredit.setText(RpInfo_deencredit.getInstance().getServiceFee_deencredit()+" "+currency);

    }

    public void toPay_deencredit(){
        String path = OthersInfo_deencredit.getInstance_deencredit().getRpPath_deencredit();
        if (!TextUtils.isEmpty(path)) startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(path)));
    }

}